
var rootPath = getRootPath();

var longitude,latitude,province,city;

$(document).ready(function(){
	
	$("head").append("<meta name='viewport' content='width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no'>");
	$("head").append("<link href='" + rootPath + "/views/gmmcActivity/ActivityCommon/css/common.css' rel='stylesheet' type='text/css' />");
	$("head").append("<link href='" + rootPath + "/views/gmmcActivity/ActivityCommon/css/loadding.css' rel='stylesheet' type='text/css' />");
//	$("head").append("<script src=\"http://api.map.baidu.com/api?v=2.0&ak=nrCmUgvjHhmWNdnLOe4Uvgc1v8hWX9kQ&callback=initialize\"></script>");
	
	jQuery.getScript(rootPath + "/js/common/common.js", function(data, status, jqxhr) {
		jQuery.getScript(rootPath + "/js/common/wechatCommon.js");
	});
	
});

function initialize() {
	if (navigator.geolocation) {
		var geolocation = new BMap.Geolocation();
		geolocation.getCurrentPosition(function(r) {
			if (this.getStatus() == BMAP_STATUS_SUCCESS) {
				var centerPoint = new BMap.Point(r.longitude, r.latitude);
				var gc = new BMap.Geocoder();
				gc.getLocation(centerPoint, function(rs) {
					var addComp = rs.addressComponents;
					province = addComp.province;
				});
			}
		}, {enableHighAccuracy : true});
	}
}

function getRootPath(){
    var currentPath = window.document.location.href; 
    var pathName = window.document.location.pathname;  
    var pos = currentPath.indexOf(pathName);
    var localhostPaht = currentPath.substring(0,pos);
    return(localhostPaht+"/wx");  
}

// 显示loadding
function showLoadding(){
	var html = '';
	html += "<div class='loadding'>";
		html += "<div class='loadding-wrap'>";
			html += "<img src='" + rootPath + "/views/gmmcActivity/ActivityCommon/images/che.png' alt='' class='block auto loading_img'>";
			html += "<div class='che inline-block left-c'><img src='" + rootPath + "/views/gmmcActivity/ActivityCommon/images/zuo.png' alt=''></div>";
			html += "<div class='che inline-block right-c'><img src='" + rootPath + "/views/gmmcActivity/ActivityCommon/images/you.png' alt=''></div>";
			html += "<img class='auto' src='" + rootPath + "/views/gmmcActivity/ActivityCommon/images/loading.png' alt=''>";
		html += "</div>";
	html += "</div>";
	$("body").append(html);
	$(".loadding").fadeIn(100,function(){
		hideLoadding();
	});
}

// 删除loadding
function hideLoadding(){
	$(".loadding").fadeOut(1000,function(){
		setTimeout(function(){
			var display = $('.loadding').css('display');
			if(display == 'none'){
				$('.loadding').remove();
			}
		},3000);
	});
}

// 居中的小黑框
function showAlert(str) {
	if ($("#commonAlert").length == 0) {
		var html = '';
		html += "<div id='commonAlert' style='	position: fixed;left:50%;top:50%;z-index:9999;transform: translate(-50%, -50%);width:195px;min-height: 30px;max-height: 120px;padding: 10px 20px;font-size: 12px;overflow: hidden;background: #000;color: #fff;text-align: center;background: rgba(0,0,0,0.9);border-radius: 10px;display:none;'>";
		html += str;
		html += "</div>";
		$("body").append(html);
	} else {
		$("#commonAlert").html(str);
	}
	$("#commonAlert").fadeIn(3000, function(){
		hideAlert();
	});
}
function initRandom(n,m) {
	var w = m-n;
	var num = parseInt(Math.random()*w + n,10);
	return num;

}
//居中的小黑框,带回调函数
function showAlert(str,callback) {
	if ($("#commonAlert").length == 0) {
		var html = '';
		html += "<div id='commonAlert' style='	position: fixed;left:50%;top:50%;z-index:9999;transform: translate(-50%, -50%);width:195px;min-height: 30px;max-height: 120px;padding: 10px 20px;font-size: 12px;overflow: hidden;background: #000;color: #fff;text-align: center;background: rgba(0,0,0,0.9);border-radius: 10px;display:none;'>";
		html += str;
		html += "</div>";
		$("body").append(html);
	} else {
		$("#commonAlert").html(str);
	}
	$("#commonAlert").fadeIn(1500, function(){
		hideAlert();
		callback;
	});
}

//居中的小黑框,自定义消失
function showAlert(str,callback,isAutomaticHide) {
	if ($("#commonAlert").length == 0) {
		var html = '';
		html += "<div id='commonAlert' style='	position: fixed;left:50%;top:50%;z-index:9999;transform: translate(-50%, -50%);width:195px;min-height: 30px;max-height: 120px;padding: 10px 20px;font-size: 12px;overflow: hidden;background: #000;color: #fff;text-align: center;background: rgba(0,0,0,0.9);border-radius: 10px;display:none;'>";
		html += str;
		html += "</div>";
		$("body").append(html);
	} else {
		$("#commonAlert").html(str);
	}
	$("#commonAlert").fadeIn(1500, function(){
		callback;
		isAutomaticHide ? function(){} : hideAlert();
	});
}

// 隐藏小黑框
function hideAlert() {
	$("#commonAlert").fadeOut(1000);
}

// 半透明黑屏
function showOverlay() {
	
	$("body").append("<div id='overlay' class='myOverlay'></div>");
	
    $("#overlay").height(pageHeight());
    $("#overlay").width(pageWidth());
    $("#overlay").fadeTo(200, 0.5);
}

function hideOverlay() {
    $("#overlay").fadeOut(200,$("#overlay").remove());
}

function pageHeight() {
    return document.body.scrollHeight;
}

function pageWidth() {
    return document.body.scrollWidth;
}

function abort(){
	hideAlert();
}


var myAjax = {
	myRequest : function(murl, mdata, method, isAsync, success, error, mtimeout) {
		mdata = urlEncode(mdata);
		$.ajax({
			url: murl.indexOf("?")!=-1 ? murl + "&randomNum=" + parseInt(10000 * Math.random()) + mdata : murl + "?randomNum=" + parseInt(10000 * Math.random()) + mdata,
			dataType : "json",
            type: method || "POST",
            async: isAsync || false,
            timeout: mtimeout || 10000,
            contentType:"application/json",
            headers:{'Cookie':document.cookie},
            success: function (data) {
                success ? success(data):function(){};
            },
            error: function (data) {
            	error ? error(data):function(){
            		toastrErrorMessage("请求失败,请联系系统管理员");
            	};
            },
            beforeSend: function(){
				showLoadding();
			},
			complete : function(XMLHttpRequest,textStatus){
				hideLoadding();
				if(textStatus == 'timeout'){
					toastrErrorMessage("请求超时,请检查网络设置");
            	} else if(textStatus == 'error'){
					toastrErrorMessage("请求异常,请联系系统管理员");
            	}
            }
        });
	},
	myRequestNotLoadding : function(murl, mdata, method, isAsync, success, error, mtimeout, notLoadding) {
		mdata = urlEncode(mdata);
		$.ajax({
			url: murl.indexOf("?")!=-1 ? murl + "&randomNum=" + parseInt(10000 * Math.random()) + mdata : murl + "?randomNum=" + parseInt(10000 * Math.random()) + mdata,
			dataType : "json",
            type: method || "POST",
            async: isAsync || false,
            timeout: mtimeout || 10000,
            contentType:"application/json",
            headers:{'Cookie':document.cookie},
            success: function (data) {
                success ? success(data):function(){};
            },
            error: function (data) {
            	error ? error(data):function(){
            		toastrErrorMessage("请求失败,请联系系统管理员");
            	};
            },
            beforeSend: function(){
            	
			},
			complete : function(XMLHttpRequest,textStatus){
				if(textStatus == 'timeout'){
					toastrErrorMessage("请求超时,请检查网络设置");
            	} else if(textStatus == 'error'){
					toastrErrorMessage("请求异常,请联系系统管理员");
            	}
            }
        });
	},
	myFileRequest : function(murl, mdata, method, isAsync, success, error, mtimeout, mformId) {
		var formData = new FormData($("#" + mformId));
		$.ajax({
			url: murl.indexOf("?")!=-1 ? murl + "&randomNum=" + parseInt(10000 * Math.random()) : murl + "?randomNum=" + parseInt(10000 * Math.random()),
			data: formData,
			dataType : "json",
			contentType: false,
			processData: false,
			type: method || "POST",
			async: isAsync || false,
			timeout: mtimeout || 10000,
			headers:{'Cookie':document.cookie},
			success: function (data) {
                success ? success(data):function(){};
            },
            error: function (data) {
            	error ? error(data):function(){
            		showAlert("请求失败,请联系系统管理员",abort());
            	};
            },
            beforeSend: function(){
				showLoadding();
			},
			complete : function(XMLHttpRequest,textStatus){
				hideLoadding();
            	if(textStatus == 'timeout'){
            		showAlert("请求超时,请检查网络设置",abort());
            	} else if(textStatus == 'error'){
            		showAlert("请求异常,请联系系统管理员",abort());
            	}
            }
		});
	},
	myJump : function(murl, mtype, mdata) {
		mdata = urlEncode(mdata);
		var url = murl.indexOf("?")!=-1 ? murl + "&randomNum=" + parseInt(10000 * Math.random()) + mdata : murl + "?randomNum=" + parseInt(10000 * Math.random()) + mdata;
		$.ajax({
			url: url,
			dataType : "json",
			type: "GET",
			timeout: 10000,
			error: function (data) {
				showAlert("请求地址错误,请联系系统管理员");
            },
			complete : function(XMLHttpRequest, textStatus){
				if(textStatus == 'timeout'){
            		showAlert("请求超时,请检查网络设置",abort());
            	} else if(textStatus == 'error'){
            		showAlert("请求异常,请联系系统管理员",abort());
            	} else {
            		if("replace" == mtype)
            			window.location.replace(url)
            		else
            			window.location.href = url;
				}
            }
		});
	}
};

var urlEncode = function (param, key, encode) {
	if(param==null || param=='') return '';
	var paramStr = '';
	var t = typeof (param);
	if (t == 'string' || t == 'number' || t == 'boolean') {
		paramStr += '&' + key + '=' + ((encode==null||encode) ? encodeURIComponent(param) : param);
	} else {
		for (var i in param) {
			var k = key == null ? i : key + (param instanceof Array ? '[' + i + ']' : '.' + i);
			paramStr += urlEncode(param[i], k, encode);
		}
	}
	return paramStr;
};

